<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;
use Myredis\Myredis;
class Login extends Controller
{
    /**
     * 加载登录界面
     * @return \think\response\View
     */
    public function index()
    {
        return view('index');
    }

    public function doLogin(Request $request)
    {
        if (!Request::instance()->isPost()) {
            return $this->error('小伙子,迷了路', url('admin/login/index'));
        }
        //接受数据
        $user = Request::instance()->post('user');
        $pwd = Request::instance()->post('pass');
        $status = Request::instance()->post('status');
        //处理数据
        $res = Db::table('xo_admin_users')->where([
            'name' => $user,
            'pwd' => md5($pwd),
            'status' => 1
        ])->field('id')->find();
        if ($res) {
            // $token = time() . mt_rand(1, 1000000);
            // MyRedis::set($res['id']. 'admintoken', $token);
            Session::set('admin', [
                'id'=>$res['id'],
                'name'=>$user,
                // 'token'=> $token
            ]);

            return $this->success('登录成功', url('admin/index/index'));
        } else {
            return $this->error('登录失败', url('admin/login/index'));
        }
    }

    /**
     * 退出登录界面
     * @return \think\response\View
     */
    public function logout()
    {
        // echo 1;
        Session::delete('admin');
        return $this->success('退出成功',url('admin/login/index'));

    }
}